package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dao.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public Result<String> remove(Long id) {

        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();

        dishWrapper.eq(Dish::getCategoryId,id);

        Map<String, Object> map;

        map = dishService.getMap(dishWrapper);


        if (map!=null)
            throw  new CustomException("关联了菜品，不能删除");

        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();

        setmealWrapper.eq(Setmeal::getCategoryId,id);

        Map<String, Object> map1;

        map1= setmealService.getMap(setmealWrapper);

        if (map1!=null)
            throw  new CustomException("关联了套餐，不能删除");

        removeById(id);

        return Result.success("删除成功");
    }
}
